	********************************************************************************
	****
	**** Date: 04.25.2022							 
	**** Update: 07.12.2023					 
	**** Author: JW josephgwright@gmail.com 				 	
	**** NOTE: This program has been executed in Stata 18.0	 
	**** 
	**** Using files in $dir: 
	****
	****  	pers-use.dta
	****  	ZA7500_v4-0-0.dta
	****  	vparties-evs-codes.do
	****
	*********************************************************************************
 
 		capture log close
		log using Ch6-Violence.log, replace

 
 	******************************
	**** Set directory, seed *****
	******************************
		set more off 
		set matsize 1000
		global seed ="984353"
		set scheme plotplain
		cd "$dir"
		
		
	********************************
	*** Election violence trends ***
	********************************
		use pers-use,clear
		tsset cowcode year 
		gen opolar = l1polar if year==minyr
		egen ipolar = max(opolar),by(lid)
		*drop if GNB==1
		drop b	
		gen majoritarian = v2elparlel==0 if v2elparlel~=.
		gen proportional = v2elparlel==1 if v2elparlel~=.
		sort cowcode year 
		gen nextyearleader = current_leader[_n+1] if cowcode ==cowcode[_n+1] 
 		gen election = v2xel_elecparl==1 | v2xel_elecpres==1
		gen exelection = v2xel_elecpres==1 | (v2xel_elecparl==1 & pres==0)
		egen std_v2x_clphy = std(v2x_clphy)
		egen std_v2elintim = std(v2elintim)

		
		* Exclude presidential elections in Parliamentary systems | Parl election in Pres system *
		replace exelection = 0 if (country=="Austria" & (year==2004 | year==2016)) | (country=="Slovakia" & (year==2009 | year==2019)) | ///
			(country=="Macedonia" & year==1999) | (country=="Croatia" & year==2009) | (country=="Bulgaria" & (year==1992 | year==1996)) | ///
			(country=="Romania" & (year==2008 | year==2016)) | (country=="Finland" & year==2018) |  ///
			(country=="Iceland" & (year==2004 | year==2012)) | (country=="Lebanon" & year==2009)
		
		tsset cowcode year
		gen fpid =f.electing_p_id
		gen f2pid =f2.electing_p_id
		gen flid = f.lid
		gen f2lid = f2.lid
 		gen xlose  = (fpid~=electing_p_id)  & (lid~=flid) if fpid~=.  & flid~=. & exelection==1
		recode xlose (0=1) if (f2pid~=electing_p_id)  & (lid~=f2lid) & f2pid~=.  & f2lid & exelection==1
		gen time  =year-1990
		gen lose = v2eltvrexo==2 if exelection~=. & v2eltvrexo~=.
		replace lose = 0 if lose==1 & lid==flid & lid==f2lid
		replace lose = 0 if xlose==0 & v2eltvrexo==2 & pres==1                    /* leader re-elected but different party */
		replace lose = 1 if xlose==1 & (v2eltvrexo==1 | v2eltvrexo==0) & pres==1  /* leader loses and different party wins executive */
		
		 * Double checked these codings *
		replace lose = 1 if (country=="Dominican Republic" & year ==2000) |  (country=="Dominican Republic" & year ==2004) |  ///
			(country=="Guatemala" & year ==1999) |  (country=="Costa Rica" & year ==1994 ) |  (country=="Costa Rica" & year ==1998 ) | ///
			(country=="Panama" & (year==1994 | year==1999)) | (country=="Ecuador" & (year==1992|year==1996|year==1998 )) | ///
			(country=="Chile" & year ==1999) | (country=="Uruguay" & year ==2019 ) | (country=="Belgium" & year==1999) | ///
			(country=="Netherlands" & (year==1994 | year==2010)) | (country=="Austria" & year==1999) | (country=="Guinea Bissau" & year==2009) | ///
			(country=="France" & year==2017) | (country=="Portugal" & year==2006) | ///
			(country=="Macedonia" & (year==1998 | year==2002 | year==2004))	| (country=="Croatia" & year==2000) |   ///
			(country=="Federal Republic of Yugoslavia" & (year==2003 | year==2008))	| (country=="Kosovo" & (year==2017 | year==2019)) | ///
			(country=="Slovenia" & (year==1992 | year==2018)) | (country=="Moldova" & year==2009) | (country=="Romania" & year==1992) | ///
			(country=="Latvia" & (year==1995 | year==1998)) | (country=="Ukraine" & year==2004) | (country=="Iceland" & (year==1991 | year==2017)) | ///
			(country=="Benin" & (year==2006 | year==2016)) | (country=="Nigeria" & year==2015) | (country=="Malawi" & year==2014) | ///
			(country=="Madagascar" & (year==1996 | year==2001 | year==2018)) | (country=="Iraq" & year==2018) | (country=="Tunisia" & year==2014) | ///	
			(country=="Mongolia" & year==1997) | (country=="South Korea" & year==1992) | (country=="Sri Lanka" & year==2019) | ///
			(country=="Pakistan" & (year==1993 | year==1997 | year==2013))		
		replace lose = 0 if (country=="Haiti" & year==1995) | (country=="Colombia" & year==2002) | (country=="Netherlands" & year==2006) | ///
		   (country=="Belgium" & year==2007) | (country=="Japan" & year==1993) | (country=="France" & year==2002) | ///
		   (country=="Portugal" & year==2011) | (country=="Slovakia" & (year==1994 | year==2016)) | ///
		   (country=="Macedonia" & (year==2004 | year==2016 | year==2019)) | (country=="Croatia" & year==2016) | ///
		   (country=="Federal Republic of Yugoslavia" & (year==2002 | year==2007)) | (country=="Slovenia" & (year==2002)) | ///
		   (country=="Moldova" & year==2014) | (country=="Estonia" & year==2015) | (country=="Latvia" & (year==2010 | year==2014)) | ///
		   (country=="Georgia" & year==2018) | (country=="Finland" & year==1994) | (country=="Sweden" & year==2010) | ///
		   (country=="Iceland" & (year==1995 | year==2003)) | (country=="Benin" & year==2011) | (country=="Lesotho" & year==1998) | ///
		   (country=="Israel" & year==2006)
		  
		* Drop France 2017: incumbent ruling party was the Socialists but the violence occurred during 
		* the 2nd round that pitted Le Pen (National Front) against Macron (En Marche) -- and Le Pen lost
		drop if country=="France" & year==2017
		   
  		* Election violence outcome *
		gen elviolence = v2elpeace*-1
		gen eviolence =  v2elpeace_ord<4 if v2elpeace_ord~=.
		gen elviolence_ord = v2elpeace_ord*-1 + 5
		tab elviolence_ord
		list country year elviolence_ord persparty lose if  (elviolence_ord==4 | elviolence_ord==5) & exelection==1,clean noobs
		
		* Rescale state-led violence for same mean/sd as non-state violence *
		sum frepress  if exelection==1 & lose~=.
		local m1 = r(mean)
		local sd1=r(sd)
		sum eviolence  if exelection==1  & lose~=.
		local m2 = r(mean)
		local sd2=r(sd)
		replace frepress = (frepress+abs(`m1')+`m2' + ((abs(`m1')+`m2')/(`sd1'/.9))) /(`sd1'/`sd2')
		sum frepress eviolence  if exelection==1  & lose~=.
		
 		label def region 1 "Eastern Europe and Central Asia" 2 "Latin America and the Caribbean"  ///
			3 "Middle East and North Africa" 4 "Sub-Saharan Africa" 5 "Western Europe and North America" ///
			6 "Asia and Pacific" ,replace
		label val pregion region
		table pregion, stat(mean eviolence)
		keep if exelection==1  & lose~=.
		
 
		* Summary stats *
		tab  eviolence lose,row col
		table pregion if exelection==1 & lose~=.,stat(n lose) stat(mean lose) stat(mean eviolence)   stat(mean  frepress)
		
		* Election violence time trends plot *
		global bw=4
		global v= "eviolence"
		sum  eviolence if lose~=.
		twoway   lpoly $v year,bw($bw)lpat(solid)lcol(gs1)ylab(.2(.1).4) tit(All elections) ///
			ytit(Incidence of election violence)xtit(Year) saving(h1.gph,replace)  
			
		twoway (lpoly eviolence year if exelection==1,ytit("Election violence") ///
			ylab(.15(.15).45)bw($bw)xtit(Year)) ///
			(lpoly frepress year if exelection==1,lpat(solid)bw($bw) ///
 			legend(lab(1 "Non-state violence")lab(2 "State-led repression")pos(6)ring(0)) ///
 			tit(State and non-state election violence))

 		twoway  (lpoly $v year if lose==0,bw($bw)lpat(solid)lcol(gs10)) ///
			(lpoly $v year if lose==1,saving(h2.gph,replace)   ///
			bw($bw)lpat(solid)lcol(gs10)lcol(gs1)ylab(.2(.1).4) tit(By incumbent victory/loss) ///
			ytit(Incidence of election violence)xtit(Year)   ///
			legend(lab(1 "Ruling party {bf:wins}")lab(2 "Ruling party {bf:loses}")pos(11)ring(0)size(small)))	
		gr combine h1.gph h2.gph  , note("589 Executive elections, 1991-2020", ///
			size(vsmall)pos(6))col(2) xsize(6)iscale(.9)tit(Non-state election violence)
		gr export "$dir\golden\Ch6-Election-Violence-Trends.pdf",as(pdf)replace 		
	
	*******************************************
	*** Non-state election-related violence ***
	*******************************************
		global cvar  = "v2elintim ivdem ld pres major l1v2x_clphy l1v2caviol time"

		* No interaction *
		logit eviolence lose persparty if exelection==1,cluster(lid)	
		est store viol0
		logit eviolence lose persparty v2elintim if exelection==1,cluster(lid)	
		est store viol1
		logit eviolence lose persparty $cvar if exelection==1,cluster(lid)	
		est store viol2
		* Interaction *
		gen loseXpers = lose*persparty
		logit eviolence persparty lose loseXpers $cvar if exelection==1,cluster(lid)
		est store viol3
		lincom lose + loseXpers*.3
		lincom lose + loseXpers*.7
		
		label var lose `""Incumbent" "loses    " "election  ""'
		label var persparty `""Ruling party " "{bf:personalism}""'
		label var v2elintim  `""Government" "electoral   " "intimidation ""'
		label var loseXpers `""Incumbent loses" "X           " "Personalism  ""'

		coefplot (viol0, msymbol(d))(viol1, msymbol(P)) (viol2, msymbol(T)) (viol3, msymbol(Oh)), order(persparty)  ///
				drop(_cons  ivdem ld pres majoritarian l1v2x_clphy l1v2caviol time) xline(0) msymbol(d) mfcolor(white) grid(glcolor(gs15)) ///
				levels(95 90) legend(lab(3 "Baseline")lab(6 "+ Govt intimidation") ///
				lab(9 "+ covariates")lab(12 "Interaction")  order(3 6 9 12) ///
				size(small) pos(6) col(2) ring(1)) xsize(2) ysize(2) xlab(-3(3)6)  ///
				xtitle("        Coefficient estimate", size(small))  ///
				ciopts(lwidth(thin)) aspectratio(1.1) scale(.75) title(Election violence, size(medium) height(2))
		gr export "$dir\golden\T-Election-Violence-Estimates.pdf",as(pdf)replace 		

		* Split sample *		
		logit eviolence persparty $cvar if exelection==1 & lose==0 
		logit eviolence persparty $cvar if exelection==1 & lose==1 
		
		* Kernel *
		krls eviolence lose persparty $cvar if exelection==1,d(k)lambda(.4065 )
		twoway lpolyci k_lose persparty,yline(0)legend(off)bw(.15)ytit(Marginal effect of losing an election)xtit(Ruling party personalism)
		drop k_*

		* Plot interaction *
		gen x=.
		gen b=.
		gen hi5=.
		gen lo5=.
		gen n=_n
		xi:interflex eviolence lose persparty $cvar if exelection==1,cluster(lid)nbin(2)cutoffs(.545)
			mat list r(estBin)
		xi:interflex eviolence lose persparty $cvar if exelection==1,type(kernel)bw(.22) 
			mat list r(margeff)
			mat r =r(margeff)
			forval i=1/50 {
				qui replace x  = r[`i',1] if n==`i'
				qui replace b  = r[`i',2] if n==`i'
				qui replace hi5  = r[`i',5] if n==`i'
				qui replace lo5  = r[`i',4] if n==`i'
			}
		 twoway (hist persparty if e(sample)==1,ytit(" ",axis(2))yaxis(2)yscale(range(0 50)axis(2))ylab(0 " ",axis(2))) ///
			(rarea hi5 lo5 x if x<=50,sort col(gs14) yaxis(1)yscale(alt)yscale(alt axis(2)) ) ///
			(line b x if x<=50,sort lcol(gs1)lpat(solid)yline(0)ylab(-.1(.1).3) ///
			xtit(Ruling party personalism)legend(off)ytit("Marginal effect of incumbent {bf:losing an election}",size(small)) ///
			tit("Incumbent losing an election increases election violence" "when the ruling party is personalist" ) ///
			note("580 Executive elections, 1991-2020",	size(small)pos(6))) 
		 gr export "$dir\golden\Ch6-Election-Related-Non-State-Violence.pdf",as(pdf)replace 		
 
		* Show ruling party personalism does not boost election loss government intimidation *
		xi:interflex std_v2elintim lose persparty ivdem ld pres l1v2x_clphy l1v2caviol time if exelection==1, 
			mat r =r(estBin)
			forval i=1/3 {
				qui replace x  = r[`i',1] if n==`i'
				qui replace b  = r[`i',2] if n==`i'
				qui replace hi5  = r[`i',5] if n==`i'
				qui replace lo5  = r[`i',4] if n==`i'
			}
		 twoway  (rspike hi5 lo5 x if n<=3,sort col(gs1)   ) ///
			(scatter b x if n<=3,sort yline(0)ylab(-.1(.1).4)xlab(0(.2)1) ///
			xtit(Ruling party personalism)legend(off)ytit("Marginal effect of incumbent {bf:losing an election}",size(small)) ///
			tit("Government intimidation" ) ///
			note("579 Executive elections, 1991-2020",	size(small)pos(6))saving(h1.gph,replace)) 		
			
		xi:interflex std_v2x_clphy lose persparty ivdem ld pres l1v2x_clphy l1v2caviol time if exelection==1,
			mat r =r(estBin)
			forval i=1/3 {
				qui replace x  = r[`i',1] if n==`i'
				qui replace b  = r[`i',2] if n==`i'
				qui replace hi5  = r[`i',5] if n==`i'
				qui replace lo5  = r[`i',4] if n==`i'
			}
		 twoway  (rspike hi5 lo5 x if n<=3,sort col(gs1)   ) ///
			(scatter b x if n<=3,sort yline(0)ylab(-.1(.1).4)xlab(0(.2)1) ///
			xtit(Ruling party personalism)legend(off)ytit("Marginal effect of incumbent {bf:losing an election}",size(small)) ///
			tit("Government repression" ) ///
			note("579 Executive elections, 1991-2020",	size(small)pos(6))saving(h2.gph,replace)) 	
		gr combine h1.gph h2.gph,xsize(8)tit("Incumbent losing associated with government intimidation" "but not government repression")
		gr export "$dir\golden\T-Election-Repression-Intimidation.pdf",as(pdf)replace 	
			
		* Exclude Africa *
		xi:interflex eviolence lose persparty $cvar if exelection==1 & (cowcode<400 | cowcode>=600),type(kernel)bw(.22)  
			mat r =r(margeff)
			forval i=1/50 {
				qui replace x  = r[`i',1] if n==`i'
				qui replace b  = r[`i',2] if n==`i'
				qui replace hi5  = r[`i',5] if n==`i'
				qui replace lo5  = r[`i',4] if n==`i'
			}
		 twoway (hist persparty if e(sample)==1,ytit(" ",axis(2))yaxis(2)yscale(range(0 50)axis(2))ylab(0 " ",axis(2))) ///
			(rarea hi5 lo5 x if x<=50,sort col(gs14) yaxis(1)yscale(alt)yscale(alt axis(2)) ) ///
			(line b x if x<=50,sort lcol(gs1)lpat(solid)yline(0)ylab(-.1(.1).5) ///
			xtit(Ruling party personalism)legend(off)ytit("Marginal effect of incumbent {bf:losing an election}",size(small)) ///
			tit("Exclude Africa") ///
			note("505 Executive elections, 1991-2020",	size(small)pos(6))saving(h1.gph,replace)) 
		* Exclude Europe *
		xi:interflex eviolence lose persparty $cvar if exelection==1 & (cowcode<200 | cowcode>=400),type(kernel)bw(.22)  
			mat r =r(margeff)
			forval i=1/50 {
				qui replace x  = r[`i',1] if n==`i'
				qui replace b  = r[`i',2] if n==`i'
				qui replace hi5  = r[`i',5] if n==`i'
				qui replace lo5  = r[`i',4] if n==`i'
			}
		 twoway (hist persparty if e(sample)==1,ytit(" ",axis(2))yaxis(2)yscale(range(0 50)axis(2))ylab(0 " ",axis(2))) ///
			(rarea hi5 lo5 x if x<=50,sort col(gs14) yaxis(1)yscale(alt)yscale(alt axis(2)) ) ///
			(line b x if x<=50,sort lcol(gs1)lpat(solid)yline(0)ylab(-.1(.1).5) ///
			xtit(Ruling party personalism)legend(off)ytit("Marginal effect of incumbent {bf:losing an election}",size(small)) ///
			tit("Exclude Europe" ) ///
			note("306 Executive elections, 1991-2020",	size(small)pos(6))saving(h2.gph,replace)) 
			
		gr combine h1.gph h2.gph,xsize(8)tit("Incumbent losing an election increases election violence" "when the ruling party is personalist")
		gr export "$dir\golden\T-Election-Violence-Global.pdf",as(pdf)replace 		

	***********************************************
	*** Losing party accepting an election loss ***
	***********************************************
	 twoway lpolyci v2elaccept persparty if exelection==1 & lose==1
 	 gen notaccept = v2elaccept_ord<3 if v2elaccept_ord~=0 & v2elaccept_ord~=.
	 ttest notaccept if exelection==1,by(create)
	 krls notaccept  persparty       if exelection==1 & lose==1 
	 krls notaccept  persparty $cvar if exelection==1 & lose==1 
 	 krls notaccept  persparty $cvar l1polar if exelection==1 & lose==1 
	 krls v2elaccept persparty $cvar if exelection==1 & lose==1

	*********************************************************** 
	*** Ruling Party personalism increases violent rhetoric ***
	*********************************************************** 
	twoway lpolyci v2paviol persparty
	reg v2paviol persparty,cluster(lid)
	reghdfe v2paviol persparty,a(year)cluster(lid)
	reghdfe v2paviol persparty,a(cowcode)cluster(lid)
	reghdfe v2paviol persparty,a(cowcode year)cluster(lid)
	reghdfe v2paviol persparty ld ivdem,a(cowcode year)cluster(lid)
	reghdfe v2paviol persparty ld ivdem i_pop,a(cowcode year)cluster(lid)
	reghdfe v2paviol persparty ld ivdem ipi,a(cowcode year)cluster(lid)
	reghdfe v2paviol persparty ld ivdem ipolar,a(cowcode year)cluster(lid)
	reghdfe v2paviol persparty ld ivdem i_pop ipi ipolar,a(cowcode year)cluster(lid)
			

 	
	
*******************************************************************
*********** EVS data on justify political violence ****************
*******************************************************************	
use "$dir\ZA7500_v4-0-0.dta",clear

tab c_abrv  if country==643 | country==112 | country==31 | country==51
drop if   country==643 | country==112 | country==31 | country==51
egen panel = group(country year mode)
egen local = group(country year mode v275b_N2)

recode v100 (3=0) (2=.5) (1=1) (-1 -2=0),gen(protest)
recode v211 (5=0) (4=.25) (3=.5) (2=.75) (1=1) (-2 -1 0 =.),gen(socialmedia)
recode v210 (5=0) (4=.25) (3=.5) (2=.75) (1=1) (-2 -1 0 =.),gen(newspaper)
recode v209 (5=0) (4=.25) (3=.5) (2=.75) (1=1) (-2 -1 0 =.),gen(radio)
recode v208 (5=0) (4=.25) (3=.5) (2=.75) (1=1) (-2 -1 0 =.),gen(tv)
recode v162 (-2 -1 0 =.) , gen(polviolence)
recode v148  (4=0) (3=.333) (2=.667) (1=1) (-2 -1 0 =.) , gen(demsupport)
recode v145  (4=0) (3=.333) (2=.667) (1=1) (-2 -1 0 =.) , gen(strongmansupport)
recode v172 (1=1) (2=.5) (-1 -2 3=0) (7=.),gen(vote)

gen emp_fulltime = v244==1 if v244~=.
gen emp_retired = v244==5  if v244~=.
gen emp_unemp = v244==8  if v244~=.
gen xage = age if age>=18
gen native = v227==1
gen educ_med=v243_r==2
gen educ_high=v243_r==3
gen income_med=v261_r==2
gen incom_high=v261_r==3
gen female = v225==2
gen rightid = v102>=6 & v102<=10
gen leftid =v102>=1 & v102<=4
gen noconfidencepolparties = v130==3 | v130==4

  label copy V174_CS mylabel2
  gen pid = v174_cs
  label value pid mylabel2
  egen tag = tag(cntry_y pid)
  gen pid2 = pid
  sort c_abrv
  browse pid pid2 c_abrv year if tag==1
  
  **********************************************
  *** Get Varieties of Parties partyid codes ***
  **********************************************
  gen v2paid=.
  qui do vparties-evs-codes.do
  sum year
  sort v2paid year
  save temp,replace

use "$dir\pers-use.dta",clear
tsset cowcode year
gen incv2paid = v2paid
gen jan1persparty =persparty
gen dec31persparty = f.persparty 
gen jan1rulingpers = v2paind_ord==3 | v2paind_ord==4 if v2paind_ord~=.
replace v2paind = f.v2paind if v2eltvrexo==2
rename v2paind rulingv2paind
 	** Attacks on the State **
		gen ojud = l1v2x_jucon if year==min
		egen ijud = max(ojud),by(lid)
		 alpha v2jupurge v2jupoatck v2jupack,item std gen(attack)
		 replace attack = attack*-1
		 qui sum attack
		 replace attack = (attack +abs(r(min)))/(r(max) + abs(r(min)))
		 sum attack
		 hist attack
		 tsset lid year
		 tssmooth ma l12attacks = attack,window(2 0 0)
	** Accept election outcome **
		 tsset lid year
		 gen accept = v2elaccept
		 replace accept = l1.v2elaccept if accept==.
		 replace accept = l2.v2elaccept if accept==.
		 replace accept = l3.v2elaccept if accept==.
		 replace accept = l4.v2elaccept if accept==.
sort cowcode year
save pers-temp,replace

use "$dir\vdem-parties-merge.dta",clear
keep if year>=2017 & year<=2020
merge v2paid year using temp
tab _merge
drop _merge
rename COW cowcode
sort cowcode year
merge cowcode year using pers-temp
tab _merge
tab country if _merge==1
tab country if persparty==. | polviolence==.
keep if persparty~=. & polviolence~=.  
	
 ********************************************************
  egen group=group(local v2paid)
  gen tempseatshare =v2paseat
  gen z = v2panoallian/v2panumbseat if  v2panoallian~=. & v2panumbseat~=. 
  sum z
  replace tempseat = z if z~=. & z>v2paseat
  egen maxseat = max(tempseat),by(country) 
  gen loser =tempseat<maxseat if tempseat~=. & maxseat~=.
  tab loser
  qui sum v2paind
  replace v2paind = (v2paind+abs(r(min)))/(abs(r(min))+r(max))
  tab v2paind_ord
  gen personalist = v2paind_ord==3 | v2paind_ord==4 if v2paind_ord~=.
  gen antielite = v2paanteli_ord==3 | v2paanteli_ord==4 if v2paanteli_ord~=.
  gen people = v2papeople_ord==3 | v2papeople_ord==4 if v2papeople_ord~=.
  replace xage = xage*.1
  recode v176 (-10 -9 -8 -7 -4 -2 -1 0=.),gen(unfairvotecount)
  tab v176 unfairvote,m
  recode v181 (-10 -9 -8 -7 -4 -2 -1 0=.),gen(unfairelection)
  tab v181 unfairelection,m
  gen anyviolence = polviolence>=5 if polviolence~=.
  tab polviolence anyviolence,m
  
*********************************************************** 
  qui centile persparty  
  local cut = r(c_1)
  global cut = `cut'
  global dvar = "xage female educ* emp* incom*"
  drop b
  
    * No evidence that personalist voters justify violence more *
  ttest anyviolence,by(personalist)
  qui melogit anyviolence personalist||country:
	lincom personalist
  qui melogit anyviolence personalist $dvar||country:
	lincom personalist
  qui mixed anyviolence personalist||country:
	lincom personalist
  qui mixed anyviolence personalist $dvar||country:
	lincom personalist
	
* Losers more likely to justify violence *
	mixed anyviolence loser ||country:
	mixed anyviolence $dvar loser ||country:

 * Average personalist voter effect on political violence is zero but positive effect for losing election *
  mixed anyviolence personalist loser $dvar||country:
	est store v1  
 * Now look at whether personalist voters are more likely than nonpersonalist voters to endorse violence when they lose **
	xtsum anyviolence personalist  if lose==0,i(country)  /* no within variation in personalist voter among winners */
	reg anyviolence personalist $dvar if lose==0          /* pooled effect is 0.6 percent */
	mixed anyviolence personalist $dvar if lose==0||country:  /* partial pooled is 0.4 percent */
	replace personalist= personalist*2
  mixed anyviolence personalist $dvar if lose==0||country:
	est store v2
	recode personalist (2=1)
  mixed anyviolence personalist $dvar if lose==1||country:
	est store v3
  mixed anyviolence personalist v2xpa_popul $dvar if lose==1||country:
	est store v4
	
	label var personalist  `""{bf:Personalist}" "{bf:party}    " "{bf:voter}   ""'  
	label var loser "Election loser"
	label var female "Female"
	label var xage "Age"
	label var leftid "Left self id"
	label var rightid "Right self id"
	label var educ_med `""Medium   " "education" "(low)   ""'
	label var income_med `""Medium  " "income" "(low)  ""'
	label var educ_high `""High   " "education" "(low)   ""'
	label var incom_high `""High  " "income" "(low)  ""'
	label var v2xpa_popul "Populism"
	coefplot(v1, msymbol(O))(v2, msymbol(S))(v3, msymbol(T))  , ///
		drop(_cons emp_fulltime emp_retired emp_unemp) ///
		grid(glcolor(gs15))xline(0,lpattern(dash)) xlab(-.04(.02).04) ///
		xtitle(Coefficient estimates,size(small))order(loser personalist $dvar loser)level(90 95) title("Justify political violence", ///
		size(medium)height(6))xsize(2) ysize(3.5)mlabel format(%9.2g) ///
		mlabsize(vsmall)mlabposition(11)mlabgap(*.75) ///
		legend(lab(3 "All voters")lab(6 "Election winners") lab(9 "Election losers")      ///
		order(3 6  9)size(vsmall)pos(6)col(4)ring(1)note(" " "Respondents from 28 countries in the European Values Survey." ///
		"Constant and employment indictators included but not reported." "Mixed effects linear model.",size(small)pos(6))) 
 	gr export "$dir\golden\Ch6-EVS-Justify-Violence.pdf",as(pdf)replace 

	
	coefplot(v1, msymbol(O))   , ///
		drop(_cons emp_fulltime emp_retired emp_unemp) ///
		grid(glcolor(gs15))xline(0,lpattern(dash)) xlab(-.04(.02).04) ///
		xtitle(Coefficient estimates,size(small))order(loser personalist $dvar loser)level(90 95) title("Justify political violence", ///
		size(medium)height(6))xsize(2) ysize(3.5)mlabel format(%9.2g) ///
		mlabsize(vsmall)mlabposition(11)mlabgap(*.75) ///
		legend(lab(3 "All voters")lab(6 "Election winners") lab(9 "Election losers")      ///
		order(3 6  9)size(vsmall)pos(6)col(4)ring(1)note(" " "Respondents from 28 countries in the European Values Survey." ///
		"Constant and employment indictators included but not reported." "Mixed effects linear model.",size(small)pos(6))) 
 	gr export "$dir\golden\T-EVS-Justify-Violence1.pdf",as(pdf)replace 
		
		
		coefplot(v1, msymbol(O))(v2, msymbol(S))(v3, msymbol(T))  , ///
		drop(_cons $dvar loser emp_fulltime emp_retired emp_unemp) ///
		grid(glcolor(gs15))xline(0,lpattern(dash)) xlab(-.04(.02).04) ///
		xtitle(Coefficient estimates,size(small))order( personalist)level(90 95) title("Justify political violence", ///
		size(medium)height(6))xsize(2) ysize(3.5)mlabel format(%9.2g) ///
		mlabsize(vsmall)mlabposition(11)mlabgap(*.75) ///
		legend(lab(3 "All voters")lab(6 "Election winners") lab(9 "Election losers")      ///
		order(3 6  9)size(vsmall)pos(6)col(4)ring(1)note(" " "Respondents from 28 countries in the European Values Survey." ///
		"Constant and employment indictators included but not reported." "Mixed effects linear model.",size(small)pos(6))) 
 	gr export "$dir\golden\T-EVS-Justify-Violence2.pdf",as(pdf)replace 
		
* Check with additional specifications *
   mixed anyviolence personalist                      if lose==1||country:
   mixed anyviolence personalist leftid rightid $dvar if lose==1||country:
   mixed anyviolence personalist native $dvar if lose==1||country:
   mixed anyviolence personalist v2paminor $dvar if lose==1||country:
   mixed anyviolence personalist v2xpa_popul $dvar if lose==1||country:
   mixed anyviolence personalist v2paviol $dvar if lose==1||country:

* Interaction term among winners and losers *
  mixed anyviolence C.personalist##C.jan1pers loser $dvar ||country: 
  lincom personalist + c.personalist#c.jan1persparty*.2
  lincom personalist + c.personalist#c.jan1persparty*.7
* Interaction term among losers *
  mixed anyviolence C.personalist##C.jan1pers $dvar if loser==1||country:
  lincom personalist + c.personalist#c.jan1persparty*.2
  lincom personalist + c.personalist#c.jan1persparty*.7
  
		   ** Lack of within variation for personalist party voters for some countries **
		  xtsum personalist if cowcode==385 |cowcode==390 |cowcode==380 |cowcode==375 |cowcode==344 ///
			|cowcode==339 |cowcode==255 |cowcode==235 |cowcode== 200,i(cowcode)
		  gen novar = cowcode==385 |cowcode==390 |cowcode==380 |cowcode==375 |cowcode==344 ///
			|cowcode==339 |cowcode==255 |cowcode==235 |cowcode== 200
		  tab novar

		  qui reghdfe anyviolence personalist jan1persparty leftid rightid $dvar if loser==1 & novar==0,a(country)  
		  table country_name if e(sample)==1,stat(sd personalist)
		  qui replace novar=1 if country_name=="Poland" | country_name=="Austria"
		  
		  * Check that average effect of personalist among loses is the same when dropping countries with no personalist within variation *
		  mixed anyviolence personalist $dvar if lose==1   ||country:
		  mixed anyviolence personalist $dvar if lose==1 & novar==0 ||country:

* Reported interaction with kernel estimator * FE so drop countries with no within variation in personalist voter
  reghdfe anyviolence C.personalist##C.jan1persparty   $dvar if loser==1,a(country)
    lincom personalist + c.personalist#c.jan1persparty*.2
	lincom personalist + c.personalist#c.jan1persparty*.7
  reghdfe anyviolence C.personalist##C.jan1persparty   $dvar if loser==1 & novar==0,a(country)
    lincom personalist + c.personalist#c.jan1persparty*.2
	lincom personalist + c.personalist#c.jan1persparty*.7
  interflex anyviolence personalist jan1persparty $dvar if loser==1,fe(panel) type(kernel)bw(.25)
	mat list r(margeff) 
  interflex anyviolence personalist jan1persparty $dvar if loser==1 & novar==0,fe(panel) type(kernel)bw(.25)
	mat list r(margeff) 	/* Losing personalist party voter moves from 0% to 4% more likely to justify violence than nonpersonalist losing voter */ 
  mat r = r(margeff)
  gen x=.
  gen b=.
  gen hi=.
  gen lo=.
  gen n=_n
  forval i=1/50 {
  	qui replace x  = r[`i',1] if n==`i'
  	qui replace b  = r[`i',2] if n==`i'
  	qui replace hi  = r[`i',5] if n==`i'
  	qui replace lo  = r[`i',4] if n==`i'
  }
  
twoway (rarea hi lo x,col(gs14)) (line b x,lcol(gs1)lpat(solid)yline(0,lpat(solid))ylab(0(.02).08) ///
	xtit(Ruling party personalism)legend(off)ytit("Marginal effect of a {bf:personalist party voter}",size(small)) ///
	tit("Personalist voter effect" ) ///
	note(Sample of voters who backed a losing political party in election, ///
	size(small)pos(6))saving(h1.gph,replace)) 
 	gr export "$dir\golden\T-EVS-Justify-Violence-PP1.pdf",as(pdf)replace 
	
	 * Check when measuring RULING party personalism with V-Parties data *
	reghdfe anyviolence C.personalist##C.ruling   $dvar if loser==1 & novar==0,a(country) 
		lincom personalist + c.personalist#c.ruling *(.22 - 1.33)
		lincom personalist + c.personalist#c.ruling *(.22 + 1.33)
	reghdfe anyviolence C.personalist##C.ruling $dvar if loser==1,a(country)
		lincom personalist + c.personalist#c.ruling *(.22 - 1.33)
		lincom personalist + c.personalist#c.ruling *(.22 + 1.33)
	reghdfe anyviolence C.personalist##C.ruling $dvar if loser==1,a(country)cluster(v2paid)
		lincom personalist + c.personalist#c.ruling *(.22 - 1.33)
		lincom personalist + c.personalist#c.ruling *(.22 + 1.33)
		
	* Adjust for types of populist party *
	interflex polviolence personalist jan1persparty antielite people rightid leftid $dvar ///
		if loser==1 & novar==0,fe(country)type(kernel)bw(.25)
	mat list r(margeff)

  * Split data by personalist voter *
	  interflex anyviolence loser jan1persparty   $dvar if personalist==0,fe(country) type(kernel)bw(.3)
	  mat r=r(margeff)
	  gen b1=.
	  gen hi1=.
	  gen lo1=.
	   forval i=1/50 {
		qui replace x  = r[`i',1] if n==`i'
		qui replace b1  = r[`i',2] if n==`i'
		qui replace hi1  = r[`i',5] if n==`i'
		qui replace lo1  = r[`i',4] if n==`i'
	  }
	  interflex anyviolence loser jan1persparty   $dvar if personalist==1,fe(country) type(kernel)bw(.3)
	  mat r=r(margeff)
	  gen b2=.
	  gen hi2=.
	  gen lo2=.
	   forval i=1/50 {
		qui replace x  = r[`i',1] if n==`i'
		qui replace b2  = r[`i',2] if n==`i'
		qui replace hi2  = r[`i',5] if n==`i'
		qui replace lo2  = r[`i',4] if n==`i'
	  }

	twoway (rarea hi1 lo1 x,col(gs14)) (rarea hi2 lo2 x,col(gs14))  (line b1 x,lcol(gs1)lpat(dash))  ///
		(line b2 x,lcol(gs1)lpat(solid)yline(0,lpat(solid)lcol(gs10))ylab(-.05(.05).15) ///
		xtit(Ruling party personalism)legend(off)ytit("Marginal effect of a {bf:voting for losing party}",size(small)) ///
		tit(Loser effect) note("Sample of all voters: winners & losers",size(small)pos(6)) ///
		text(0.09 0.7 "Personalist" "voters  ",place(e)size(.3cm)) ///
		text(-0.05 0.5 "Non-personalist" "voters  ",place(e)size(.3cm))saving(h2.gph,replace))
 	gr export "$dir\golden\T-EVS-Justify-Violence-PP2.pdf",as(pdf)replace 
	gr combine h1.gph h2.gph,col(2)xsize(8)tit("Personalist party voters more likely to justify political violence when losing ruling party is more personalist")
	gr export "$dir\golden\Ch6-EVS-Justify-Violence-Party-Personalism.pdf",as(pdf)replace 
		 
  

* Checks by split samples: high/low ruling party personalism and loser *
  centile jan1persparty  if personalist~=. & anyviolence~=.,centile(50)
  global cut = r(c_1) + .0001
  mixed anyviolence personalist v2xpa_popul $dvar if loser==0 & jan1persparty<$cut ||country: 
  		xtsum personalist if e(sample),i(cowcode)
  reg anyviolence personalist v2xpa_popul $dvar if loser==0 & jan1persparty<$cut  
		lincom personalist
		qui replace b = r(estimate) if n==1
		qui replace hi = r(ub) if n==1
		qui replace lo = r(lb) if n==1	 
  mixed anyviolence personalist v2xpa_popul $dvar if loser==1 & jan1persparty<$cut ||country: 
  		xtsum personalist if e(sample),i(cowcode)
		lincom personalist 								 
		qui replace b = r(estimate) if n==4
		qui replace hi = r(ub) if n==4
		qui replace lo = r(lb) if n==4	 
  mixed anyviolence personalist v2xpa_popul $dvar if loser==0 & jan1persparty>$cut ||country: 
		xtsum personalist if e(sample),i(cowcode)
  reg anyviolence personalist v2xpa_popul $dvar if loser==0 & jan1persparty>$cut
		lincom personalist
		qui replace b = r(estimate) if n==2
		qui replace hi = r(ub) if n==2
		qui replace lo = r(lb) if n==2	 
  mixed anyviolence personalist v2xpa_popul $dvar if loser==1 & jan1persparty>$cut ||country:
  		xtsum personalist if e(sample),i(cowcode)
		lincom personalist 								/* non-personalist loser; reference==nonpers winner */
		qui replace b = r(estimate) if n==5
		qui replace hi = r(ub) if n==5
		qui replace lo = r(lb) if n==5	 
  gen rb=round(b,.001)
  replace b=. if n==3
  replace hi=. if n==3
  replace lo=. if n==3
  twoway (rspike lo hi n if n<=5,lcol(blue*.45))(scatter b n if n<=5,mlab(rb)mcol(blue)msy(O)yline(0,lcol(gs12)lp(dash)) ///
		xscale(range(0.7 5.3)) ///
		xlab(1 `""Non-personalist" "ruling party"  "Winners""' ///
		2 `""{bf:Personalist}" "ruling party"  "Winners""'  ///
		4 `""Non-personalist" "ruling party"  "Losers""'  ///
		5 `""{bf:Personalist}" "ruling party"  "{bf:Losers}""') ///
		xtit(" ")legend(off) ytit("Marginal effect of personalist party Voter",size(medium))ylab(-.08(.04).04) ///
		tit("Justifying political violence",size(medium)))
 	
* Check with ordered outcome variable *
  tab polviolence
  meologit polviolence C.personalist##C.jan1pers $dvar if lose==1 || country:
  lincom personalist + c.personalist#c.jan1persparty*.2
  lincom personalist + c.personalist#c.jan1persparty*.7
 
* Additional specification for interaction term *
  mixed anyviolence C.personalist##C.jan1pers if loser==1||country:
  lincom personalist + c.personalist#c.jan1persparty*.2
  lincom personalist + c.personalist#c.jan1persparty*.7
   mixed anyviolence C.personalist##C.jan1pers native  leftid rightid  $dvar if loser==1||country:
  lincom personalist + c.personalist#c.jan1persparty*.2
  lincom personalist + c.personalist#c.jan1persparty*.7
  mixed anyviolence C.personalist##C.jan1pers v2paminor v2xpa_popul v2paviol $dvar if loser==1||country:
  lincom personalist + c.personalist#c.jan1persparty*.2
  lincom personalist + c.personalist#c.jan1persparty*.7
  
 * Check against populist voter effect *
  reghdfe anyviolence C.personalist##C.jan1pers populism   $dvar if loser==1 & novar==0,a(country)
 	  lincom personalist + c.personalist#c.jan1persparty*.2
	  lincom personalist + c.personalist#c.jan1persparty*.7  
  reghdfe anyviolence C.personalist##C.jan1pers C.populism##C.jan1pers $dvar if loser==1 & novar==0,a(country)
	  lincom personalist + c.personalist#c.jan1persparty*.2
	  lincom personalist + c.personalist#c.jan1persparty*.7  
	  
 * Can't calculate bandwidth via cross-validation with perfect collinear between interaction and FE *
 forval i =1/6 {
 	local i =`i'/10
 	interflex anyviolence personalist jan1persparty v2xpa_popul $dvar if loser==1 & novar==0,fe(country)type(kernel)bw(`i')
	mat list r(margeff)
 }

erase h1.gph
erase h2.gph
erase .pdf
erase temp.dta
erase pers-temp.dta

	***************************** THE END **************************
	
	log close